<?php
namespace app\admin\service;

use library\tools\Node;
use think\Db;
use think\facade\Request;
use app\admin\service\CompanyService;
use app\admin\service\PointService;

/**
 * 减员服务
 * Class DownsizingService
 * @package app\admin\service
 */
class DownsizingService
{
	
    /**
     * 根据个人ID获取正在进行中的增员id
     * @param int $person_id 个人ID
     * @return array
     */
    public static function getIncrease($person_id)
    {
    	$person = Db::name('system_person')->where(['id'=>$person_id])->find();
		$insurance = Db::name('system_increase')->where(['person_id'=>$person_id,'company_id'=>$person['company_id'],'is_deleted'=>0,'status'=>2])->order('id desc')->find();	
		if($insurance){
			$increase_id = $insurance['id'];
		}else{
			$increase_id = 0;
		}
		return $increase_id;
    }
    
	
    /**
     * 减员提交，扣除费用
     * @param array $id  减员表id
     * @return array
     */
    public static function deduction($id)
	{
		$service = Db::name('system_service')->where(['is_special'=>2])->find();
		$info = Db::name('system_downsizing')->where(['id'=>$id])->find();
		$contract = Db::name('system_contract')->where(['id'=>$info['contract_id'],'status'=>1])->find();
		if($contract['setmeal_type'] == 1){
			$company = Db::name('system_company')->where(['id'=>$info['company_id']])->find();
			//找到主公司
			$master = CompanyService::getMaster($info['company_id']);
			$desc = $info['code'].'-'.$service['title'];		
			if($info['deduction'] == 1){
				//扣除服务点			
				$re = PointService::pointDetAdd($master['mid'],$company['mid'],$info['contract_id'],2,$service['point'],$desc,$master['unique']);
				if($re){
					Db::name('system_company')->where(['mid'=>$master['mid']])->setDec('point', $service['point']);
				}
			}else{
				//扣除服务次数
				$re = PointService::frequencyDetAdd($master['mid'],$company['mid'],$info['contract_id'],$service['id'],2,1,$desc,$master['unique']);
				if($re){
					Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setDec('surplus');
					Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setInc('used');
				}
			}
		}
			
	}


	/**
     * 减员待审核删除，返还扣除费用
     * @param array $ids
     * @return array
     */
    public static function deduction_back($ids)
	{
		$service = Db::name('system_service')->where(['is_special'=>2])->find();
		$list = Db::name('system_downsizing')->where(['id'=>$ids])->field('id,code,company_id,contract_id,deduction,point')->select();
		if($list){
			foreach($list as $k=>$v){
				//找到主公司
				$master = CompanyService::getMaster($v['company_id']);
				$company = Db::name('system_company')->where(['id'=>$v['company_id']])->find();
				$desc = $v['code'].'-'.$service['title'];
				if($v['deduction']){
					if($v['deduction'] == 1){
						//返还点数			
						$re = PointService::pointDetAdd($master['mid'],$company['mid'],$v['contract_id'],3,$v['point'],$desc,$master['unique']);
						if($re){
							Db::name('system_company')->where(['mid'=>$master['mid']])->setInc('point', $v['point']);
						}
					}else{
						//返还次数
						$re = PointService::frequencyDetAdd($master['mid'],$company['mid'],$v['contract_id'],$service['id'],3,1,$desc,$master['unique']);
						if($re){
							Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setInc('surplus');
							Db::name('system_member_count')->where(['member_id'=>$master['mid'],'service_id'=>$service['id']])->setInc('back');
						}
					}
				}						
			}
		}
				
	}
    
    
    /**
     * 导出减员
     * @param array $id
     * @return array
     */
    public static function export_downsizing($id)
    {
    	$filename = '减员服务列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','电话号码'),
            array('company_name','公司名称'),
            array('end_date','截止日期'), 
            array('yanglao_et','养老截止日期'), 
            array('yanglao_desc','养老备注信息'), 
            array('gongshang_et','工伤截止日期'),
            array('gongshang_desc','工伤备注信息'), 
            array('shiye_et','失业截止日期'), 
            array('shiye_desc','失业备注信息'), 
            array('yiliao_et','医疗截止日期'), 
            array('yiliao_desc','医疗备注信息'), 
            array('gongjijin_et','公积金截止日期'),
            array('gongjijin_desc','公积金备注信息'),          
            array('source','数据来源'),
            array('status','状态'), 
            array('create_at','委托日期'),          
            array('csr_id','所属客服'),
            array('desc','备注信息'),
        );
    	$list = Db::name('system_downsizing')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$person_ids = Db::name('system_downsizing')->where(['id'=>$id])->column('person_id');
			$names = Db::name('system_person')->where(['id'=>$person_ids])->column('name','id');
			$id_numbers = Db::name('system_person')->where(['id'=>$person_ids])->column('id_number','id');
			$mobiles = Db::name('system_person')->where(['id'=>$person_ids])->column('mobile','id');
			$companys = Db::name('system_company')->column('name','id');			
			foreach($list as $k=>$v){
				$downsizing_insurance_et = Db::name('system_downsizing_insurance')->where(['increase_id'=>$v['increase_id']])->column('end_date','insurance');
				$downsizing_insurance_desc = Db::name('system_downsizing_insurance')->where(['increase_id'=>$v['increase_id']])->column('desc','insurance');
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $names[$v['person_id']];
				$arr[$k]['id_number'] = ' '.$id_numbers[$v['person_id']];
				$arr[$k]['mobile'] = $mobiles[$v['person_id']];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
	    		$arr[$k]['end_date'] = $v['end_date'];
	    		$arr[$k]['yanglao_et'] = isset($downsizing_insurance_et[1]) ? $downsizing_insurance_et[1] : '';
	    		$arr[$k]['yanglao_desc'] = isset($downsizing_insurance_desc[1]) ? $downsizing_insurance_desc[1] : '';
	    		$arr[$k]['gongshang_et'] = isset($downsizing_insurance_et[2]) ? $downsizing_insurance_et[2] : '';
	    		$arr[$k]['gongshang_desc'] = isset($downsizing_insurance_desc[2]) ? $downsizing_insurance_desc[2] : '';
	    		$arr[$k]['shiye_et'] = isset($downsizing_insurance_et[3]) ? $downsizing_insurance_et[3] : '';
	    		$arr[$k]['shiye_desc'] = isset($downsizing_insurance_desc[3]) ? $downsizing_insurance_desc[3] : '';
	    		$arr[$k]['yiliao_et'] = isset($downsizing_insurance_et[4]) ? $downsizing_insurance_et[4] : '';
	    		$arr[$k]['yiliao_desc'] = isset($downsizing_insurance_desc[4]) ? $downsizing_insurance_desc[4] : '';
	    		$arr[$k]['gongjijin_et'] = isset($downsizing_insurance_et[5]) ? $downsizing_insurance_et[5] : '';
	    		$arr[$k]['gongjijin_desc'] = isset($downsizing_insurance_desc[5]) ? $downsizing_insurance_desc[5] : '';	    		
				$arr[$k]['source'] = ['1'=>'后台','2'=>'PC网站','3'=>'移动H5'][$v['source']];
				$arr[$k]['status'] = ['1'=>'待审核','2'=>'已审核','3'=>'已反馈'][$v['status']];
				$arr[$k]['create_at'] = date('Y-m-d',strtotime($v['create_at']));
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['desc'] = $v['desc'];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }


	
	/**
     * 减员确认
     * @param string $increase_id
	 * @param string $person_id
     * @param string $insurance
     * @param string $downins_id
     * @param string $status
     * @param string $end_date
	 * @param string $account_period
	 * @param string $desc
     * @return number
     */
    public static function downsizing_confirm($increase_id,$person_id,$insurance,$downins_id,$status,$end_date,$account_period,$desc)
	{
		$arr = [
			'downins_id' => $downins_id,		
			'status' => $status,									
			'end_date' => $end_date,
			'account_period' => $account_period,
			'desc' => $desc,
		];
		$re = Db::name('system_insurance_log')->where(['increase_id'=>$increase_id,'person_id'=>$person_id,'insurance'=>$insurance])->update($arr);
		if($re){			
			Db::name('system_insurance_result')->where(['person_id'=>$person_id,'insurance'=>$insurance])->update($arr);	
			//修改增员过程表
			Db::name('system_increase_insurance')->where(['increase_id'=>$increase_id,'insurance'=>$insurance])->update(['status'=>0]);					
		} 
	}
	
	
	
	/**
     * 导出确认信息
     * @param array $id
     * @return array
     */
    public static function export_downsizing_insurance($id)
    {
    	$filename = '减员确认列表';
    	$titles = array(
    		array('code','服务编号'),
            array('name','姓名'),
            array('id_number','身份证号'),
            array('mobile','电话号码'),
            array('company_name','公司名称'),                       
            array('insurance','保险类型'),     
            array('end_date','截止日期'), 
            array('account_period','所属账期'), 
            array('area','所属区域'),   
            array('status','状态'),           
            array('confirm_date','确认日期'),
            array('csr_id','所属客服'),
            array('ssr_id','所属社保'),
            array('desc','备注信息'),
        );
    	$list = Db::name('system_downsizing_insurance')->where(['id'=>$id])->order('id desc')->select();
		if(!empty($list) && $list){
			$companys = Db::name('system_company')->column('name','id');
			foreach($list as $k=>$v){
				$increase = Db::name('system_increase')->where(['id'=>$v['increase_id']])->find();
	    		$arr[$k]['code'] = $v['code'];
	    		$arr[$k]['name'] = $increase['name'];
				$arr[$k]['id_number'] = ' '.$increase['id_number'];
				$arr[$k]['mobile'] = $increase['mobile'];
				$arr[$k]['company_name'] = $companys[$v['company_id']];
				$arr[$k]['insurance'] = get_option('insurance')[$v['insurance']];
				$arr[$k]['end_date'] = $v['end_date'];
				$arr[$k]['account_period'] = $v['account_period'];
				$arr[$k]['area'] = $v['area'] ? get_option('area')[$v['area']] : '';
				$arr[$k]['status'] = ['0'=>'待审核 ','1'=>'待确认','2'=>'已确认','3'=>'已反馈'][$v['status']];
				$arr[$k]['confirm_date'] = $v['confirm_date'] ? date('Y-m-d',$v['confirm_date']) : '';
				$arr[$k]['csr_id'] = $v['csr_id'] ? user_list()[$v['csr_id']] : '';
				$arr[$k]['ssr_id'] = $v['ssr_id'] ? user_list()[$v['ssr_id']] : '';
				$arr[$k]['desc'] = $v['desc'];
	    	}
		}else{
			$arr = [];
		}   
    	
        return ['filename'=>$filename,'titles'=>$titles,'list'=>$arr];
    }
	
   
    
}
